package frame;

import Homework.Commodity;
import Homework.IUse;
import Homework.UseImp;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoLabel;
import com.lovo.netCRM.component.LovoTxt;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdateFrame extends JFrame {
    private LovoLabel codeLabel = new LovoLabel("编号",50,30,this);
    private LovoLabel nameLabel = new LovoLabel("商品名",50,70,this);
    private LovoTxt priceTxt = new LovoTxt("单价",50,110,this);
    private LovoLabel factoryLabel = new LovoLabel("厂家",50,150,this);
    private LovoLabel createDateLabel = new LovoLabel("生产日期",50,190,this);

    private IUse iUse = new UseImp();
    //修改的商品编号
    private int code;


    public UpdateFrame(int code){
        this.code = code;

        this.setLayout(null);
        this.init();

        this.setSize(400,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
    }

    private void init(){
        //先完成老数据展示。先按编号查询商品
//        System.out.println(code);
        Commodity c = iUse.codeSearch(code);
        this.codeLabel.setText(c.getCode()+"");
        this.nameLabel.setText(c.getName());
        this.priceTxt.setText(c.getPrice());
        this.factoryLabel.setText(c.getFactory());
        this.createDateLabel.setText(c.getDate().toString());

        LovoButton upDateButton = new LovoButton("修改",150,230,this);
        upDateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                upDateCommodity();
            }
        });



    }

    private void upDateCommodity(){
        //调用业务方法，完成修改
        iUse.updatePrice(code,priceTxt.getText());
        this.dispose();
        new MainFrame();
    }




}
